package com.monas.client.export;

import java.sql.Types;

import org.apache.poi.ss.usermodel.CellStyle;

/**
 * Class excel 2003/2007 untuk melakukan ekspor dengan format plain tabel.
 * 
 * @author 'EkoWahyudi'
 * 
 */
public class ExcelPlainTable extends AbstractExcel {

	public ExcelPlainTable(String excelExtension) {
		super(excelExtension);
	}

	@Override
	public void render() {
		// Buat header
		createHeader();

		// Buat body
		createBody();
	}

	/**
	 * Buat badan tabel.
	 */
	private void createBody() {
		// baca per baris
		for (int i = 0; i < records.size(); i++) {

			Object[] record = records.get(i);

			checkSheetLimitation();

			// membuat baris baru pada tabel.
			rows = workSheet.createRow(rows.getRowNum() + 1);

			for (int j = 0; j < headers.length; j++) {
				int no = j;
				Object value = record[j];
				CellStyle styleCell = getStyleCell(i, j);

				int type = headers[j].getSQLType();

				// Isi cell sesuai dengan tipenya.
				if (value != null) {
					if (type == Types.NUMERIC || type == Types.REAL
							|| type == Types.INTEGER
							|| value instanceof Integer) {
						// Tipe integer, dkk.
						createIntegerCell(no, styleCell,
								Integer.parseInt((String) value));
					} else if (type == Types.FLOAT || value instanceof Float
							|| value instanceof Double || type == Types.REAL) {
						// Tipe float, dkk.
						createFloatCell(no, styleCell,
								Float.valueOf((String) value));
					} else {
						createStringCell(no, styleCell, value.toString());
					}
				} else {
					createBlankCell(no, styleCell);
				}

			}
		}
		
		// rapikan lebar kolom.
		resizeWorkSheet();
	}

}
